<template>
	<view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="input">
				<image src="../../../static/images/pasd.png"></image>
				<input type="number" name="oldpasd" placeholder="输入旧密码"/>
			</view>
			<view class="input">
				<image src="../../../static/images/pasd.png" ></image>
				<input type="number" name="newpasd1" placeholder="设置新密码"/>
			</view>
			<view class="input">
				<image src="../../../static/images/pasd.png" ></image>
				<input type="number" name="newpasd2" placeholder="再次输入密码"/>
			</view>
			
			<button class="submit" form-type="submit">完成</button>
		</form>
		
	</view>
</template>

<script>
	import util from '../../../utils/util.js'
	import { mineModule } from "../../../modules/mine.js"
	const Mine = new mineModule()
	
	export default {
		data() {
			return {

			};
		},
		methods: {
			formSubmit(e) {
				let { oldpasd, newpasd1, newpasd2 } = e.detail.value
				const isState =  oldpasd && newpasd1 && newpasd2
				this._onCheck(isState) // 检验是否 都填写完整
					.then(()=>{
						return this._judgePasd(newpasd1,newpasd2) // 检验两次密码是否完整
					})
					.then(()=>{
						return this._pasdCheck(newpasd2) // 密码校验
					})
					.then(()=>{
						return Mine.postUpdatePwd(oldpasd,newpasd2) // 提交修改密码
					})
					.then((res)=>{ // 修改密码成功回调
						uni.showToast({
							icon: "success",
							title: '修改成功'
						})
						setTimeout(()=>{
							uni.removeStorageSync('userData') // 清除缓存用户信息， 返回 登录页
							uni.reLaunch({
								url: '/pages/login/login/login'
							})
						},1000)
					})
					.catch((res)=>{ // 所有错误信息处理回调
						util.tost(res)
					})
			},
			
			_onCheck (isState) { // 检验是否 都填写完整
				return new Promise((resolve, reject)=>{
					isState? resolve() : reject('请填写完整')
				})
			},
			_judgePasd(newpasd1,newpasd2) { // 检验两次密码是否完整
				return new Promise((resolve, reject)=>{
					newpasd1 == newpasd2? resolve() : reject('两次输入的密码不一致，请检查')
				})
			},
			_pasdCheck(newpasd2) { // 密码校验
				return new Promise((resolve, reject)=>{
					const isOk =  util.checkPassword(newpasd2)
					 if (isOk ) resolve() 
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "../../../common/scss/common.scss";

	page{
		background-color: #F7F7F7;
	}
	form{
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: ws(40);
	}
	.input{
		background-color: #fff;
		height: ws(50);
		display: flex;
		align-items: center;
		padding: 0 ws(15);
		margin-bottom: ws(15);
		border-radius: ws(5);
		> input{
			height: ws(16);
			line-height: ws(40);
			font-size: ws(16);
			border-left: 1upx solid #ddd;
			margin-left: ws(15);
			padding-left: ws(15);
			flex: 1;
		}
		> image{
			width: ws(25);
			height: ws(25);
		}
	}
	.submit{
		position: absolute; 
		left: 0;
		right: 0;
		bottom: ws(100);
		width: 80%;
		margin: auto;
		background-color: #2A83FF;
		border: none !important;
		border-radius: ws(5);
		color: #fff;
		font-size: ws(16);
	}
</style>
